﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlServerCe;
using System.Text;
using Entities;

namespace Distribution.Data
{
    public class DPolygon
    {
        #region - Public methods -

        public static List<EPolygon> GetAll(PolygonTypeEnum polygonType)
        {
            SqlCeConnection _Con = null;
            StringBuilder query = new StringBuilder();

            try
            {
                _Con = Helper.GetConnection();

                query.Append("SELECT PolygonId, ParentPolygonFk, PolygonTypeFk, PolygonName ");
                query.Append("FROM tb_Utility_Polygons ");
                query.Append("WHERE PolygonTypeFk = @PolygonTypeFk ");

                SqlCeCommand command = new SqlCeCommand(query.ToString(), _Con);
                command.CommandType = CommandType.Text;
                command.Parameters.Add(Helper.CreateIntParameter("@PolygonTypeFk", ParameterDirection.Input, false, (int)polygonType));

                SqlCeDataReader dr = command.ExecuteReader();

                List<EPolygon> polygons = new List<EPolygon>();

                while (dr.Read())
                {
                    EPolygon polygon = new EPolygon();

                    polygon.Id = dr.GetInt32(dr.GetOrdinal("PolygonId"));
                    polygon.Type = polygonType;

                    if (!dr.IsDBNull(1))
                    {
                        polygon.Parent = new EPolygon();
                        polygon.Parent.Id = dr.GetInt32(1);
                    }

                    polygon.Name = dr.GetString(dr.GetOrdinal("PolygonName"));

                    polygons.Add(polygon);
                }

                return polygons;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        public static EPolygon GetByIdType(int id, PolygonTypeEnum polygonType)
        {
            SqlCeConnection _Con = null;
            StringBuilder query = new StringBuilder();

            try
            {
                _Con = Helper.GetConnection();

                query.Append("SELECT PolygonId, ParentPolygonFk, PolygonTypeFk, PolygonName ");
                query.Append("FROM tb_Utility_Polygons ");
                query.Append("WHERE PolygonTypeFk = @PolygonTypeFk AND PolygonId = @PolygonId");

                SqlCeCommand command = new SqlCeCommand(query.ToString(), _Con);
                command.CommandType = CommandType.Text;
                command.Parameters.Add(Helper.CreateIntParameter("@PolygonTypeFk", ParameterDirection.Input, false, (int)polygonType));
                command.Parameters.Add(Helper.CreateIntParameter("@PolygonId", ParameterDirection.Input, false, id));

                SqlCeDataReader dr = command.ExecuteReader();
                EPolygon polygon = new EPolygon();

                if (dr.Read())
                {
                    polygon.Id = dr.GetInt32(dr.GetOrdinal("PolygonId"));
                    polygon.Type = polygonType;

                    if (!dr.IsDBNull(1))
                    {
                        polygon.Parent = new EPolygon();
                        polygon.Parent.Id = dr.GetInt32(1);
                    }

                    polygon.Name = dr.GetString(dr.GetOrdinal("PolygonName"));
                }

                return polygon;
            }
            finally
            {
                if (_Con != null)
                {
                    _Con.Close();
                }
            }
        }

        #endregion
    }
}
